package dao

import (
	"golang.org/x/net/context"
	"mini-app/internal/dal"
)

type IPuppetUserFilter interface {
	fmtFilter(ctx context.Context, tx dal.IPuppetUserDo, q *dal.Query) dal.IPuppetUserDo
}

var _ IPuppetUserFilter = (*PuppetUserFilter)(nil)

type PuppetUserFilter struct {
	ID       int64
	NickName string
	Gender   *int
	Deleted  *bool
}

// 筛选器
func (p *PuppetUserFilter) fmtFilter(ctx context.Context, tx dal.IPuppetUserDo, q *dal.Query) dal.IPuppetUserDo {
	if p.ID != 0 {
		tx = tx.Where(q.PuppetUser.ID.Eq(p.ID))
	}
	if p.NickName != "" {
		tx = tx.Where(q.PuppetUser.NickName.Like("%" + p.NickName + "%"))
	}
	if p.Gender != nil {
		tx = tx.Where(q.PuppetUser.Gender.Eq(*p.Gender))
	}
	if p.Deleted != nil {
		tx = tx.Where(q.PuppetUser.Deleted.Is(*p.Deleted))
	}
	return tx
}

type PuppetUserListFilter struct {
	ID          int64
	NickName    string
	Gender      *int
	Deleted     *bool
	ExcludedIDs []int64
}

func (p *PuppetUserListFilter) fmtFilter(ctx context.Context, tx dal.IPuppetUserDo, q *dal.Query) dal.IPuppetUserDo {

	if p.ID != 0 {
		tx = tx.Where(q.PuppetUser.ID.Eq(p.ID))
	}
	if p.NickName != "" {
		tx = tx.Where(q.PuppetUser.NickName.Like("%" + p.NickName + "%"))
	}
	if p.Gender != nil {
		tx = tx.Where(q.PuppetUser.Gender.Eq(*p.Gender))
	}

	if p.Deleted != nil {
		tx = tx.Where(q.PuppetUser.Deleted.Is(*p.Deleted))
	} else {
		//默认只查询未被删除的
		tx = tx.Where(q.PuppetUser.Deleted.Is(false))
	}

	if len(p.ExcludedIDs) > 0 {
		tx = tx.Where(q.PuppetUser.ID.NotIn(p.ExcludedIDs...))
	}

	return tx
}
